package com.server.push.mqtt;

import com.alibaba.fastjson2.JSON;
import com.server.dao.MobileAppDao;
import com.server.dao.MobileAppTrackDao;
import com.server.http.service.MobileAppService;
import com.server.libs.PositionUtil;
import com.server.libs.login.data.AppCache;
import com.server.libs.login.AppOline;
import com.server.pojo.MobileApp;
import com.server.pojo.MobileAppTrack;
import com.server.push.dto.request.data.RequestLocation;
import com.server.push.mqtt.base.Topic;
import com.server.push.mqtt.base.annotation.MqttController;
import com.server.push.mqtt.base.annotation.MqttSubscribe;
import com.server.push.mqtt.config.MqttGateway;
import com.server.push.mqtt.tool.MQTTMsg;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Map;

@MqttController
@Slf4j
public class MqttMobileAppController {
    public final static String client = "GuardForce/MobileApp/{auth}/client";
    @Autowired
    MqttGateway mqttGateway;

    @Autowired
    MobileAppService mobileAppService;

    @Autowired
    MobileAppDao mobileAppDao;

    @Autowired
    MobileAppTrackDao mobileAppTrackDao;

    @Autowired
    AppOline appOline;


    @MqttSubscribe(topic = "GuardForce/MobileApp/{auth}/position",qos = 0)
    @Transactional
    public void position(Topic topic, String msg){
        RequestLocation data = JSON.to(RequestLocation.class,msg);
        Map<String, Object> recvTopicMap = topic.getReceivedMap();
        String auth = (String) recvTopicMap.get("auth");

        MobileApp mobileApp = mobileAppService.get(auth);
        if (mobileApp == null) {
            return ;
        }

//        log.info("位置上传：{}", mobileApp.getAccount());
        if (data.getLatitude() != null && data.getLongitude() != null) {
            mobileApp.setLatitude(data.getLatitude());
            mobileApp.setLongitude(data.getLongitude());
            mobileApp.setLocationAt(new Date());
            if (mobileApp.getTrack().isEmpty()){
                MobileAppTrack mobileAppTrack = new MobileAppTrack().setMobileApp(mobileApp);
                mobileAppTrackDao.save(mobileAppTrack);
            }else {
                MobileAppTrack last = mobileApp.getTrack().get(0);
                double v = PositionUtil.calculateDistance(last.getLatitude(), last.getLongitude(), mobileApp.getLatitude(), mobileApp.getLongitude());
                if (v>1) {
                    MobileAppTrack mobileAppTrack = new MobileAppTrack().setMobileApp(mobileApp);
                    mobileAppTrackDao.save(mobileAppTrack);
                }
            }

        }
        mobileAppDao.save(mobileApp);
    }


    @MqttSubscribe(topic = "GuardForce/MobileApp/{auth}/heart",qos = 0)
    @Transactional
    public void heart(Topic topic, String msg){
        Map<String, Object> recvTopicMap = topic.getReceivedMap();
        String auth = (String) recvTopicMap.get("auth");
        AppCache cache = mobileAppService.getCache(auth);
        if (cache!=null) {
            appOline.save(cache);
        }
    }



    @MqttSubscribe(topic = "GuardForce/MobileApp/{auth}/service",qos = 0)
    @Transactional
    public void service(Topic topic, String msg){
        MQTTMsg mqttMsg = JSON.to(MQTTMsg.class,msg);
        if (mqttMsg.getDate()>new Date().getTime() + 30*1000) return;
        if (mqttMsg.getType().equals("heart"))heart(topic, mqttMsg.getData());
        else if(mqttMsg.getType().equals("position"))position(topic, mqttMsg.getData());
    }

}
